﻿// -----------------------------------------------------------------------
// <copyright file="UniformNoise.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImageP
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    /// 
   public struct imgList
    {
       public int x;
       public int y;
    }
    public class UniformNoise
    {
        public List<imgList> imagebuffer = new List<imgList>();
        public int hMulW;
        Random rand = new Random();
        public Bitmap AddingUniform(double a, double b, double percent,Bitmap img)
        {
            double noise;
            int[,] mu = new int[img.Height, img.Width];
            RGBCOLORS[,] realImage = new RGBCOLORS[img.Height, img.Width];
            RGBCOLORS[,] noisyImage = new RGBCOLORS[img.Height, img.Width];
            imgList imR = new imgList();
            Bitmap bm = new Bitmap(img.Width, img.Height);
            hMulW = img.Height * img.Width;
        
            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    realImage[i, j].R = img.GetPixel(j, i).R;
                    realImage[i, j].G = img.GetPixel(j, i).G;
                    realImage[i, j].B = img.GetPixel(j, i).B;
                }
            }

            mu.Initialize();
            perfectRandomArray(img);
            noise = (Convert.ToDouble(1 / Convert.ToDouble (b - a)) * img.Width  * img.Height * Convert.ToDouble(percent / 100) );
            for (int g = (int) a; g < (int)b; g++)
            {
                for (int i = 0; i <= (int)noise; i++)
                {
                    imR = perfectRandom();
                    mu[imR.x, imR.y] = g;
                }   
            }

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    noisyImage[i, j].R = realImage[i, j].R + mu[i, j];
                    noisyImage[i, j].G = realImage[i, j].G + mu[i, j];
                    noisyImage[i, j].B = realImage[i, j].B + mu[i, j];
                }
            }
            Normalize nm = new Normalize();
           RGBCOLORS[,] final = nm.Normalize_image(noisyImage, img.Height, img.Width);

           for (int i = 0; i < img.Height; i++)
           {
               for (int j = 0; j < img.Width; j++)
               {

                   bm.SetPixel(j, i, System.Drawing.Color.FromArgb(Convert.ToInt32(final[i,j].R),Convert.ToInt32(final[i,j].G),Convert.ToInt32(final[i,j].B)));

               }
           }
           return bm;
        }
        
        public imgList  perfectRandom()
        {
            imgList imR = new imgList();
            
            int randomIndex;

            randomIndex = rand.Next(imagebuffer.Count );
          imR.x = imagebuffer[randomIndex].x;
          imR.y = imagebuffer[randomIndex].y;
        
          imagebuffer.RemoveAt(randomIndex);
          return imR;
        }
        public void perfectRandomArray(Bitmap img)
        {
            imgList im = new imgList();
            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {

                    im.x = i;
                    im.y = j;
                    imagebuffer.Add(im);
                }
            }

        }
    }
}
